package com.aaa.car.service.impl;

import com.aaa.car.dao.CarCommentDao;
import com.aaa.car.service.CarCommentService;
import com.aaa.common.entity.CarComment;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
 * (CarComment)表服务实现类
 *
 * @author lei chu
 * @since 2021-02-20 16:32:16
 */
@Service("carCommentService")
public class CarCommentServiceImpl implements CarCommentService {
    @Resource
    private CarCommentDao carCommentDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public CarComment queryById(Integer id) {
        return this.carCommentDao.queryById(id);
    }

    /**
     * 通过实体作为筛选条件查询
     *
     * @param pageNum    当前页数
     * @param pageSize   每页条数
     * @param carComment 实例对象
     * @return 分页数据和信息
     */
    @Override
    public PageInfo<CarComment> queryAll(int pageNum, int pageSize, CarComment carComment) {
        return PageHelper.startPage(pageNum, pageSize)
                .doSelectPageInfo(() -> this.carCommentDao.queryAll(carComment));
    }

    /**
     * 新增数据
     *
     * @param carComment 实例对象
     * @return 实例对象
     */
    @Override
    public CarComment insert(CarComment carComment) {
        carComment.setAddtime(new Date());
        carComment.setUseNum(1);
        this.carCommentDao.insert(carComment);
        return carComment;
    }

    /**
     * 修改数据
     *
     * @param carComment 实例对象
     * @return 实例对象
     */
    @Override
    public CarComment update(CarComment carComment) {
        this.carCommentDao.update(carComment);
        return this.queryById(carComment.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.carCommentDao.deleteById(id) > 0;
    }
}